package com.syezon.wifikey.wifi.info;

import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.os.Parcel;
import android.os.Parcelable;

import com.syezon.wifikey.wifi.WifiSupport;

/**
 * Created by John on 2017/4/7.
 */

public class WifiInfo implements Comparable<WifiInfo>, Parcelable {

    public static final String STATE_UNCONNECTED = "未连接";
    public static final String STATE_CONNECTING = "正在连接";
    public static final String STATE_CONNECTED = "已连接";

    private String ssid;
    private String pwd;
    private int level;
    private int signalStrength;
    private String state;  //已连接  正在连接  未连接 三种状态
    private String capabilities;//加密方式
    private WifiSupport.WifiCipherType wifiCipherType;
    private ScanResult scanResult;
    private WifiConfiguration wifiConfiguration;
    private int frequency;
    private int channel;
    private String wifiInfo;
    private String availableInfo;
    private String mac;
    private boolean isConfig;
    private boolean isAvailable;
    private boolean isConnected;

    public WifiInfo() {
    }

    protected WifiInfo(Parcel in) {
        ssid = in.readString();
        pwd = in.readString();
        level = in.readInt();
        signalStrength = in.readInt();
        state = in.readString();
        capabilities = in.readString();
        scanResult = in.readParcelable(ScanResult.class.getClassLoader());
        wifiConfiguration = in.readParcelable(WifiConfiguration.class.getClassLoader());
        frequency = in.readInt();
        channel = in.readInt();
        wifiInfo = in.readString();
        availableInfo = in.readString();
        mac = in.readString();
        isConfig = in.readByte() != 0;
        isAvailable = in.readByte() != 0;
        isConnected = in.readByte() != 0;
    }

    public static final Creator<WifiInfo> CREATOR = new Creator<WifiInfo>() {
        @Override
        public WifiInfo createFromParcel(Parcel in) {
            return new WifiInfo(in);
        }

        @Override
        public WifiInfo[] newArray(int size) {
            return new WifiInfo[size];
        }
    };

    @Override
    public String toString() {
        return "WifiInfo{" +
                "ssid='" + ssid + '\'' +
                ", level='" + level + '\'' +
                ", isConfig='" + isConfig + '\'' +
                ", pwd='" + pwd + '\'' +
                ", state='" + state + '\'' +
                ", capabilities='" + capabilities + '\'' +
                '}';
    }

    public String getCapabilities() {
        return capabilities;
    }

    public void setCapabilities(String capabilities) {
        this.capabilities = capabilities;
    }

    public String getSsid() {
        return ssid;
    }

    public void setSsid(String ssid) {
        this.ssid = ssid;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public WifiSupport.WifiCipherType getWifiCipherType() {
        return wifiCipherType;
    }

    public void setWifiCipherType(WifiSupport.WifiCipherType wifiCipherType) {
        this.wifiCipherType = wifiCipherType;
    }

    public ScanResult getScanResult() {
        return scanResult;
    }

    public void setScanResult(ScanResult scanResult) {
        this.scanResult = scanResult;
    }

    public WifiConfiguration getWifiConfiguration() {
        return wifiConfiguration;
    }

    public void setWifiConfiguration(WifiConfiguration wifiConfiguration) {
        this.wifiConfiguration = wifiConfiguration;
    }

    public int getFrequency() {
        return frequency;
    }

    public void setFrequency(int frequency) {
        this.frequency = frequency;
    }

    public int getChannel() {
        return channel;
    }

    public void setChannel(int channel) {
        this.channel = channel;
    }

    public String getWifiInfo() {
        return wifiInfo;
    }

    public void setWifiInfo(String wifiInfo) {
        this.wifiInfo = wifiInfo;
    }

    public String getAvailableInfo() {
        return availableInfo;
    }

    public void setAvailableInfo(String availableInfo) {
        this.availableInfo = availableInfo;
    }

    public String getMac() {
        return mac;
    }

    public void setMac(String mac) {
        this.mac = mac;
    }

    public boolean isConfig() {
        return isConfig;
    }

    public void setConfig(boolean config) {
        isConfig = config;
    }

    public boolean isAvailable() {
        return isAvailable;
    }

    public void setAvailable(boolean available) {
        isAvailable = available;
    }

    public boolean isConnected() {
        return isConnected;
    }

    public void setConnected(boolean connected) {
        isConnected = connected;
    }

    public int getSignalStrength() {
        return signalStrength;
    }

    public void setSignalStrength(int signalStrength) {
        this.signalStrength = signalStrength;
    }

    @Override
    public int compareTo(WifiInfo o) {
        if (isConnected) {
            if (o.isConnected) {
                return o.signalStrength - signalStrength;
            } else {
                return -1;
            }
        }
        if (isConfig) {
            if (o.isConnected) {
                return 1;
            } else {
                if (o.isConfig) {
                    return o.signalStrength - signalStrength;
                } else {
                    return -1;
                }
            }
        }
        if (o.isConnected) {
            return 1;
        }
        if (o.isConfig) {
            return 1;
        }
        return o.signalStrength - signalStrength;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(ssid);
        dest.writeString(pwd);
        dest.writeInt(level);
        dest.writeInt(signalStrength);
        dest.writeString(state);
        dest.writeString(capabilities);
        dest.writeParcelable(scanResult, flags);
        dest.writeParcelable(wifiConfiguration, flags);
        dest.writeInt(frequency);
        dest.writeInt(channel);
        dest.writeString(wifiInfo);
        dest.writeString(availableInfo);
        dest.writeString(mac);
        dest.writeByte((byte) (isConfig ? 1 : 0));
        dest.writeByte((byte) (isAvailable ? 1 : 0));
        dest.writeByte((byte) (isConnected ? 1 : 0));
    }


}
